package cn.chenxinjie.hrm.service.impl;

import cn.chenxinjie.hrm.domain.LoginUser;
import cn.chenxinjie.hrm.domain.UserMeal;
import cn.chenxinjie.hrm.dto.LoginDto;
import cn.chenxinjie.hrm.dto.refreshTokenDto;
import cn.chenxinjie.hrm.mapper.LoginUserMapper;
import cn.chenxinjie.hrm.mapper.UserMealMapper;
import cn.chenxinjie.hrm.service.ILoginUserService;
import cn.chenxinjie.hrm.util.AjaxResult;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 *  服务实现类
 *
 * @author chenxinjie
 * @since 2022-03-09
 */
@Service
public class LoginUserServiceImpl extends ServiceImpl<LoginUserMapper, LoginUser> implements ILoginUserService {

    @Autowired
    private LoginUserMapper loginUserMapper;
    @Autowired
    private UserMealMapper userMealMapper;

    @Override
    public Long entering(LoginUser loginUser) {
        // 保存loginUser到数据库,会自动返回自增主键
        loginUserMapper.insert(loginUser);
        // 再保存用户套餐信息
        UserMeal userMeal = new UserMeal();
        Long loginUserId = loginUser.getId();
        userMeal.setLoginId(loginUserId);
        userMeal.setMealId(loginUser.getMealId());
        userMeal.setExpireDate(new Date());
        userMeal.setState(0);
        userMealMapper.insert(userMeal);
        return loginUserId;
    }

    @Override
    public AjaxResult login(LoginDto dto) {
        // 参数校验 JSR303
        Integer type = dto.getType();
        // 判断账户类型
        String clientId = "";
        String clientSecret = "";
        if (type.equals(1)) {
            clientId = "admin";
            clientSecret = "1";
        } else {
            clientId = "website";
            clientSecret = "1";
        }
        String url = "http://localhost:4010/oauth/token?client_id=%s&client_secret=%s&grant_type=password&username=%s&redirect_uri=http://www.baidu.com&password=%s";
        String format = String.format(url, clientId, clientSecret, dto.getUsername(), dto.getPassword());
        String s = HttpUtil.get(format);
        Map map = JSON.parseObject(s, Map.class);
        return AjaxResult.me().setResultObj(map);
    }

    @Override
    public AjaxResult refreshToken(refreshTokenDto dto) {
        // 参数校验 JSR303
        Integer type = dto.getType();
        // 判断账户类型
        String clientId = "";
        String clientSecret = "";
        if (type.equals(1)) {
            clientId = "admin";
            clientSecret = "1";
        } else {
            clientId = "website";
            clientSecret = "1";
        }
        String url = "http://localhost:4010/oauth/token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s";
        String format = String.format(url, dto.getRefreshToken(), clientId, clientSecret);
        String s = HttpUtil.get(format);
        Map map = JSON.parseObject(s, Map.class);
        return AjaxResult.me().setResultObj(map);
    }
}
